package com.example.myapplicationndk.utils;

import android.content.Context;
import android.content.SharedPreferences;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

/**
 * Created by lenovo on 2018/5/23.
 */

public class SPUtil {
    private Context context;
    private String spName = "MDBH";
    public static final String FINGER_PRINT_SWITCH = "fingerPrintSwitch";//指纹解锁状态
    public static final String LOGIN_STATE = "loginSate";//登录状态
    public static final String IM_LOAD_H5_TOKEN = "IM_LOAD_H5_TOKEN";
    public static final String USERINFO_KEY = "user_info";//用户信息
    public static final String TOKEN = "token";//用户信息
    public static final String HISTORY_SEARCH_RECORD = "history_search_record";//搜索记录
    public static final String IS_RELEASE = "is_release";//用于存储切换环境后的值
    public static final String IS_VISIBILITY = "is_visibility";//是否显示引导图
    public static final String MAP_HISTORY_SEARCH_RECORD = "map_history_search_record";//地图搜索记录

    public static final String WEB_HISTORY_SEARCH_RECORD = "web_history_search_record";//搜索记录


    private SPUtil() {
    }

    private static final class SPUtilFactory {
        private static final SPUtil spUtil = new SPUtil();
    }

    public static SPUtil getInstance() {
        return SPUtilFactory.spUtil;
    }

    public SPUtil setSpName(Context context) {
        this.context = context;
//        this.spName = spName;
        return getInstance();
    }

    public void setString(String key, String data) {
        try {
            SharedPreferences preferences = context.getSharedPreferences(spName, Context.MODE_PRIVATE);
            SharedPreferences.Editor editor = preferences.edit();
            editor.putString(key, data);
            editor.commit();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public String getString(String key) {
        String data = "";
        try {
            SharedPreferences preferences = context.getSharedPreferences(spName, Context.MODE_PRIVATE);
            data = preferences.getString(key, "");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return data;
    }

    public void setInt(String key, int data) {
        try {
            SharedPreferences preferences = context.getSharedPreferences(spName, Context.MODE_PRIVATE);
            SharedPreferences.Editor editor = preferences.edit();
            editor.putInt(key, data);
            editor.commit();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public int getInt(String key) {
        int data = 0;
        try {
            SharedPreferences preferences = context.getSharedPreferences(spName, Context.MODE_PRIVATE);
            data = preferences.getInt(key, 0);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return data;
    }

    public void setFloat(String key, float data) {
        try {
            SharedPreferences preferences = context.getSharedPreferences(spName, Context.MODE_PRIVATE);
            SharedPreferences.Editor editor = preferences.edit();
            editor.putFloat(key, data);
            editor.commit();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public float getFloat(String key) {
        float data = 0f;
        try {
            SharedPreferences preferences = context.getSharedPreferences(spName, Context.MODE_PRIVATE);
            data = preferences.getFloat(key, 0f);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return data;
    }

    public void setLong(String key, long data) {
        try {
            SharedPreferences preferences = context.getSharedPreferences(spName, Context.MODE_PRIVATE);
            SharedPreferences.Editor editor = preferences.edit();
            editor.putLong(key, data);
            editor.commit();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public long getLong(String key) {
        long data = 0;
        try {
            SharedPreferences preferences = context.getSharedPreferences(spName, Context.MODE_PRIVATE);
            data = preferences.getLong(key, 0);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return data;
    }

    public void setBoolean(String key, boolean data) {
        try {
            SharedPreferences preferences = context.getSharedPreferences(spName, Context.MODE_PRIVATE);
            SharedPreferences.Editor editor = preferences.edit();
            editor.putBoolean(key, data);
            editor.commit();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public boolean getBoolean(String key) {
        boolean data = false;
        try {
            SharedPreferences preferences = context.getSharedPreferences(spName, Context.MODE_PRIVATE);
            data = preferences.getBoolean(key, false);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return data;
    }

    public void setMapString(String key, Set<String> value) {
        try {
            SharedPreferences sharedPreferences = context.getSharedPreferences("recordSearch", Context.MODE_PRIVATE);
            SharedPreferences.Editor editor = sharedPreferences.edit();
            editor.clear();
            editor.putStringSet(key, value);
            editor.commit();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public Set<String> getMapString(String key) {
        Set<String> value = new HashSet<String>();
        try {
            SharedPreferences sharedPreferences = context.getSharedPreferences("recordSearch", Context.MODE_PRIVATE);
            value = sharedPreferences.getStringSet(key, new HashSet<String>());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return value;
    }

    /**
     * 存储Map集合
     *
     * @param key 键
     * @param map 存储的集合
     * @param <K> 指定Map的键
     * @param <T> 指定Map的值
     */

    public <K, T> void setAdClickEventMap(Map<K, T> map) {
        if (map == null || map.isEmpty() || map.size() < 1) {
            return;
        }
        Gson gson = new Gson();
        String strJson = gson.toJson(map);

        SharedPreferences sharedPreferences = context.getSharedPreferences(spName, Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.clear();
        editor.putString("adClickEvent", strJson);
        editor.commit();
    }

    /**
     * 获取Map集合
     */
    public <K, T> Map<K, T> getAdClickEventMap() {
        Map<K, T> map = new HashMap<>();
        try {
            SharedPreferences sharedPreferences = context.getSharedPreferences(spName, Context.MODE_PRIVATE);
            String strJson = sharedPreferences.getString("adClickEvent", null);
            if (strJson == null) {
                return map;
            }
            Gson gson = new Gson();
            map = gson.fromJson(strJson, new TypeToken<Map<K, T>>() {
            }.getType());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return map;
    }

    /**
     * 清除所有数据
     */
    public void clearAll() {
        SharedPreferences preferences = context.getSharedPreferences(spName, Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = preferences.edit();
        editor.clear();
        editor.commit();
    }
}
